//
//  GYSearchVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/11.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYSearchVC.h"
#import "Head.h"
#import "DWSegmentedControl.h"
#import "GYShopProductListView.h"
#import "GYHomeSaleDetailVC.h"
#import "GYSearchViewModel.h"
#import "SearchSaleView.h"
#import "SearchBuyView.h"
#import "SearchSaleView.h"
#import "GYHomeBuyDetailVC.h"
#import "GYSearchBar.h"
#import "GYNewShopListVC.h"
@interface GYSearchVC ()<UISearchBarDelegate,DWSegmentedControlDelegate>
{
    NSInteger currentPage;
}
@property(nonatomic,strong)GYSearchBar* topSearchBar;
@property(nonatomic,strong)DWSegmentedControl *test;
@property(nonatomic,strong)UIScrollView* mainScrollView;
@property(nonatomic,strong)SearchBuyView* ShopListView;
@property(nonatomic,strong)SearchSaleView* SaleListView;
@property(nonatomic,strong)GYSearchViewModel* searchViewModel;
@property(nonatomic,strong)NSMutableDictionary* searchData;
@property(nonatomic,strong)GYNewShopListVC* NewVC;
@property(nonatomic,strong)UIView* topHeaderView;
@end

@implementation GYSearchVC
#pragma mark - getter and setter
-(GYNewShopListVC *)NewVC
{
    if (!_NewVC) {
        _NewVC=[[GYNewShopListVC alloc] init];
        _NewVC.view.frame=CGRectMake(0, 0, self.view.width, _mainScrollView.height);
    }
    return _NewVC;
}
-(NSMutableDictionary *)searchData
{
    if (!_searchData) {
        _searchData=[[NSMutableDictionary alloc] init];
    }
    return _searchData;
}
-(GYSearchViewModel *)searchViewModel
{
    if (!_searchViewModel) {
        _searchViewModel=[[GYSearchViewModel alloc] init];
    }
    return _searchViewModel;
}
-(SearchSaleView *)SaleListView
{
    if (!_SaleListView) {
        _SaleListView=[[SearchSaleView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, _mainScrollView.height)];

        @WeakSelf;
        
        _SaleListView.callBack = ^(NSString *proid) {
            GYHomeSaleDetailVC* gy=[[GYHomeSaleDetailVC alloc] init];
            gy.passID=proid;
            [selfp.navigationController pushViewController:gy animated:YES];
        };
        _SaleListView.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            currentPage = 1;
            [selfp loadData];
        }];
        _SaleListView.myTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            if (selfp.searchViewModel.SaleCanAddMore)
            {
                currentPage += 1;
                [selfp.searchData setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
                [selfp loadData];
            }else
            {
                [_SaleListView.myTable.mj_footer endRefreshingWithNoMoreData];
            }
            
        }];
        
    }
    return _SaleListView;
}
-(SearchBuyView *)ShopListView
{
    if (!_ShopListView) {
        _ShopListView=[[SearchBuyView alloc] initWithFrame:CGRectMake(self.view.width, 0, self.view.width, _mainScrollView.height)];
        @WeakSelf;
        _ShopListView.callback = ^(NSString *proid) {
            GYHomeBuyDetailVC* gy=[[GYHomeBuyDetailVC alloc] init];
            gy.passID=proid;
            [selfp.navigationController pushViewController:gy animated:YES];
        };
        
        _ShopListView.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            currentPage = 1;
            [selfp loadData];
        }];
        _ShopListView.myTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            if (selfp.searchViewModel.BuyCanAddMore)
            {
                currentPage += 1;
                [selfp.searchData setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
                [selfp loadData];
            }else
            {
                [_ShopListView.myTable.mj_footer endRefreshingWithNoMoreData];
            }
            
        }];
    }
    return _ShopListView;
}


-(UIScrollView *)mainScrollView{
    
    if (!_mainScrollView) {
        _mainScrollView=[[UIScrollView alloc] init];
        _mainScrollView.frame=CGRectMake(0, 45, self.view.width, self.view.height-45-GY_Nav_height-self.bottomShelterHeight);
        _mainScrollView.backgroundColor=[UIColor whiteColor];
        _mainScrollView.contentSize=CGSizeMake(self.view.width*2, _mainScrollView.height);
        _mainScrollView.pagingEnabled=YES;
        _mainScrollView.delegate=self;
        _mainScrollView.showsVerticalScrollIndicator=NO;
        _mainScrollView.showsHorizontalScrollIndicator=NO;
    }
    return _mainScrollView;
    
}

-(DWSegmentedControl *)test
{
    if (!_test)
    {
        _test = [[DWSegmentedControl alloc] initWithFrame:CGRectMake(58, 7.5, self.view.frame.size.width-58*2, WFCGFloatY(30))];
        _test.backgroundColor = [UIColor whiteColor];
        _test.selectedViewColor = [UIColor blueColor];
        _test.normalLabelColor = Main_Color_Red;
        _test.delegate = self;
        _test.titles = @[@"现货资源",@"求购信息"];
    }
    return _test;
}
-(UIView *)topHeaderView
{
    if (!_topHeaderView) {
        _topHeaderView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
        _topHeaderView.backgroundColor=[UIColor whiteColor];
        
        [WFFunctions WFUIaddBorderToView:_topHeaderView top:0 left:0 bottom:1 right:0 borderColor:HEXCOLOR(@"#F1F1F2") borderWidth:1];
        UIImageView* locationIV=[[UIImageView alloc] init];
        locationIV.x=self.view.frame.size.width/4-WFCGFloatX(18);
        locationIV.y=43;
        locationIV.width=WFCGFloatX(36);
        locationIV.height=2;
        locationIV.backgroundColor=HEXCOLOR(@"#B81A08");
        locationIV.tag=1000;
        
        @WeakSelf;
        
        for (int i=0; i<2; i++)
        {
            UIButton* offerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            [offerBtn setTitle:i==0?@"精品商城":@"求购信息" forState:UIControlStateNormal];
            [offerBtn setTitleColor:HEXCOLOR(@"#666666") forState:UIControlStateNormal];
            [offerBtn setTitleColor:HEXCOLOR(@"#333333") forState:UIControlStateSelected];
            offerBtn.titleLabel.font=SYSTEMFONT(15);
            [_topHeaderView addSubview:offerBtn];
            offerBtn.x=self.view.frame.size.width/2*i;
            offerBtn.y=0;
            offerBtn.width=self.view.frame.size.width/2;
            offerBtn.height=44;
            offerBtn.tag=300+i;
            if (i==0) {
                offerBtn.selected=YES;
            }
            
            [offerBtn addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                
                [selfp locationIV:tag];
            }];
            
        }
        [_topHeaderView addSubview:locationIV];
    }
    return _topHeaderView;
}
-(GYSearchBar *)topSearchBar
{
    if (!_topSearchBar) {
        _topSearchBar=[[GYSearchBar alloc] initWithFrame:CGRectMake(40, 0, self.view.width-50, 40)];
        _topSearchBar.placeholder=@"请输入关键字搜索";
        _topSearchBar.delegate=self;
        _topSearchBar.tintColor = Main_Color_Black;
        
    }
    return _topSearchBar;
}
#pragma mark - event
-(void)locationIV:(NSInteger)index
{
    for (int i=0; i<2; i++) {
        UIButton* btn=(UIButton*)[_topHeaderView viewWithTag:300+i];
        if (index==300+i) {
            btn.selected=YES;
        }else
        {
            btn.selected=NO;
        }
    }
    UIImageView* locationIV=(UIImageView* )[_topHeaderView viewWithTag:1000];
    [UIView animateWithDuration:0.3 animations:^{
        locationIV.x=self.view.frame.size.width/4-WFCGFloatX(18)+self.view.frame.size.width/2*(index-300);
    }];
    
    [_mainScrollView scrollRectToVisible:CGRectMake(self.view.width*(index-300), 0, self.view.width, _mainScrollView.height) animated:NO];
}
#pragma mark - method
-(void)loadData
{
    
    [_searchData setObject:_topSearchBar.text forKey:@"keyword"];
    
    [self.searchViewModel requestSaleData:_searchData];
    [self.searchViewModel requestBuyData:_searchData];
}


-(void)viewModelBand
{

    @WeakSelf;
    GCBaseObservationModel* buyOber=[GCBaseObservationModel new];
    buyOber.keyPath=@"buyArr";
    buyOber.observation=self.searchViewModel;
    buyOber.handler = ^(NSString *keyPath) {
        [selfp.ShopListView refresh:selfp.searchViewModel];
    };
    [self registObservation:buyOber];
    
    GCBaseObservationModel* marketOber=[GCBaseObservationModel new];
    marketOber.keyPath=@"saleArr";
    marketOber.observation=self.searchViewModel;
    marketOber.handler = ^(NSString *keyPath) {
        [selfp.SaleListView refresh:selfp.searchViewModel];
    };
    [self registObservation:marketOber];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    currentPage=1;
    [self setNavgationTheme:1];
    [self.searchData setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
    
    [self.navigationController.navigationBar addSubview:self.topSearchBar];
    
    [self viewModelBand];
   // [self loadData];
    [self.view addSubview:self.topHeaderView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    // [self.mainScrollView addSubview:self.NewVC.view];
    // [self addChildViewController:self.NewVC];
    [self.mainScrollView addSubview:self.SaleListView];
    [self.mainScrollView addSubview:self.ShopListView];
    [self.view addSubview:self.mainScrollView];
    
   
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - delegate
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    
    [_topSearchBar resignFirstResponder];
    [_topSearchBar setShowsCancelButton:NO animated:YES];
    _topSearchBar.text=@"";
    [self loadData];
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
     [_topSearchBar resignFirstResponder];
    [_topSearchBar setShowsCancelButton:YES animated:YES];
    [self loadData];
    
}
//- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
//{
//   // [searchBar setShowsCancelButton:YES animated:NO];
//    for (UIView *view in _topSearchBar.subviews.lastObject.subviews) {
//        if ([view isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
//            UIButton *cancel = (UIButton *)view;
//            [cancel setTitle:@"搜索" forState:UIControlStateNormal];
//            [cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        }
//    }
//    return YES;
//}

#pragma mark - delegate
-(void)dw_segmentedControl:(DWSegmentedControl *)control didSeletRow:(NSInteger)row
{
    NSLog(@"");
    [_mainScrollView scrollRectToVisible:CGRectMake(self.view.width*row, 0, self.view.width, _mainScrollView.height) animated:NO];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_topSearchBar resignFirstResponder];
    if (scrollView==_mainScrollView)
    {
        [_test setScrollViewOffset:scrollView.contentOffset.x/self.view.width];
        
        UIImageView* locationIV=(UIImageView* )[_topHeaderView viewWithTag:1000];

        locationIV.x=self.view.frame.size.width/4-WFCGFloatX(18)+scrollView.contentOffset.x/2;
        
        
        
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_mainScrollView)
    {
        if (scrollView.contentOffset.x/self.view.width==1)
        {
            [self locationIV:301];
        }else
        {
             [self locationIV:300];
        }
    }
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
